/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.shape.cdpointsymbol.impl;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;

import who.tmap.toolkit.shape.annotation.impl.ImageHelper;
import who.tmap.toolkit.shape.cdpointsymbol.ImageCDPointSymbol;

public class ImageCDPointSymbolImpl extends BaseCDPointSymbolImpl 
									implements ImageCDPointSymbol {
	
    private static Logger logger = Logger.getLogger(ImageCDPointSymbolImpl.class); 
	
	private ImageHelper helper =new ImageHelper();
	
	public void loadFile( File file ) throws IOException {
		helper.loadFile( file );
	}
	
	public void setImage( Image img ) {
		helper.setImage( img );
		helper.setWidth( img.getWidth( null ) );
		helper.setHeight( img.getHeight( null ) );
	}

	public void draw(Graphics g) {	
	    
	    Graphics2D g2d =(Graphics2D)g;

	    double factor =getScreenToMapFactor() * getZoomFactor() / 100d;
	    
	    Image theImage =null;
	    if ( ( width != helper.getWidth() || height != helper.getHeight() ) 
	    		&& ( (int)width != 0 && (int)height != 0 ) ) {
	    	theImage =helper.computeResizedImage( width, height );
		    //} else {
		    //	theImage =helper.getImage();
		    //}
		    
		    double theWidth =width * factor;
		    double theHeight =height * factor;	    
		    
			Point2D ptCentered =new Point2D.Double( pt.getX() - theWidth/2d, pt.getY() - theHeight/2d );
		    
		    AffineTransform at =new AffineTransform();
			
		    at.translate( ptCentered.getX(), ptCentered.getY() );
		    at.scale( factor, factor );
		    
		    g2d.drawImage( theImage, at, null );
		    
		    shape =new Rectangle2D.Double( ptCentered.getX(), ptCentered.getY(), theWidth, theHeight );
		    
		    bounds =new Rectangle2D.Double( shape.getBounds2D().getMinX(), shape.getBounds2D().getMinY() 
					  					  , shape.getBounds2D().getWidth(), shape.getBounds2D().getHeight() ); 
		    
		    displaySelectionHandles( g2d );
	    }
	}

}
